﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKLoopSearch;

namespace YJKPatternSign.StairsRoomSection.Kit
{
	internal class OutSide
	{
		public List<List<Curve>> Get(ref List<Curve> allSectionLines, Autodesk.Revit.DB.View viewSec, Autodesk.Revit.DB.Document document)
		{
			new List<List<Curve>>();
			StructureLoopSearcher structureLoopSearcher = new StructureLoopSearcher(document, 0.001);
			List<Pole2D> list = new List<Pole2D>();
			List<Curve> list2 = new List<Curve>();
			this.m_tran3DToPlane.Origin = viewSec.Origin;
			this.m_tran3DToPlane.BasisX = viewSec.RightDirection;
			this.m_tran3DToPlane.BasisY = viewSec.UpDirection;
			this.m_tran3DToPlane.BasisZ = this.m_tran3DToPlane.BasisX.CrossProduct(this.m_tran3DToPlane.BasisY);
			this.m_tran3DToPlane = this.m_tran3DToPlane.Inverse;
			for (int i = 0; i < allSectionLines.Count; i++)
			{
				Curve curve = allSectionLines[i].CreateTransformed(this.m_tran3DToPlane);
				Pole2D item = Pole2D.TransformWallToStructure2DPole(curve, null, 0.0, 0.0);
				list.Add(item);
				list2.Add(curve);
			}
			allSectionLines = list2;
			List<List<Curve>> fullFloorEdgeLoops = structureLoopSearcher.GetFullFloorEdgeLoops(list, StructureLoopSearcher.SearchEdgeLocation.SEL_Outside);
			List<List<Curve>> list3 = new List<List<Curve>>();
			foreach (List<Curve> list4 in fullFloorEdgeLoops)
			{
				List<Pole2D> list5 = new List<Pole2D>();
				foreach (Curve curve2 in list4)
				{
					Pole2D item2 = Pole2D.TransformWallToStructure2DPole(curve2, null, 0.0, 0.0);
					list5.Add(item2);
				}
				List<List<Curve>> cellsEdgeLoops = structureLoopSearcher.GetCellsEdgeLoops(list5, true);
				for (int j = 0; j < cellsEdgeLoops.Count; j++)
				{
					list3.Add(cellsEdgeLoops[j]);
				}
			}
			return list3;
		}

		public List<List<Curve>> PlaneCurveTo3D(List<List<Curve>> curvesArea)
		{
			List<List<Curve>> list = new List<List<Curve>>();
			Transform inverse = this.m_tran3DToPlane.Inverse;
			for (int i = 0; i < curvesArea.Count; i++)
			{
				List<Curve> list2 = curvesArea[i];
				List<Curve> list3 = new List<Curve>();
				for (int j = 0; j < list2.Count; j++)
				{
					Curve item = list2[j].CreateTransformed(inverse);
					list3.Add(item);
				}
				list.Add(list3);
			}
			return list;
		}

		public void PlaneCurveTo3D(ref List<CurveLoop> curvesArea)
		{
			new List<CurveLoop>();
			Transform inverse = this.m_tran3DToPlane.Inverse;
			for (int i = 0; i < curvesArea.Count; i++)
			{
				curvesArea[i].Transform(inverse);
			}
		}

		public List<List<Curve>> Curve3DToPlane(List<List<Curve>> curvesArea)
		{
			List<List<Curve>> list = new List<List<Curve>>();
			Transform inverse = this.m_tran3DToPlane.Inverse;
			for (int i = 0; i < curvesArea.Count; i++)
			{
				List<Curve> list2 = curvesArea[i];
				List<Curve> list3 = new List<Curve>();
				for (int j = 0; j < list2.Count; j++)
				{
					Curve item = list2[j].CreateTransformed(inverse.Inverse);
					list3.Add(item);
				}
				list.Add(list3);
			}
			return list;
		}

		private Transform m_tran3DToPlane = Transform.Identity;
	}
}
